/**
 * Created by gongxun on 17/1/18.
 */

var NnTableStatus = function() {
    var that = {};

    //牌桌映像
    that.mTableStatus = null;

    //录像msg
    that.replayMsgs = null;

    //录像type
    that.replayTypes = null;

    //是否是断线重练
    that.isReconnect = false;

    //设置牌桌映像
    that.setTableStatus = function(tableStatus){
        that.mTableStatus = tableStatus;

        //记录是否是断线重连
        if (that.mTableStatus.Step != 0){
            that.isReconnect = false;
        }

        //在牛牛里面才触发
        if (mo.sceneHelper.currentLayerName != "nn"){
            return;
        }

        that.updateTableStatus();
    };

    that.setReconnectStatus = function(tableStatus){
        that.mTableStatus = tableStatus;

        //记录是否是断线重连
        if (that.mTableStatus.Step == 0){
            that.isReconnect = true;
        }

        //更新用户信息
        that.updateUserInfo();

        //断线重连
        that.setReconnet();

        //ui状态
        that.setUIShowStatus();
    };

    that.updateTableStatus = function(){
        //更新用户信息
        that.updateUserInfo();

        that.updateStep();

        //ui状态
        that.setUIShowStatus();
    };

    that.updateUserInfo = function(){
        //牌局结束以后（剩余次数为0，step为0） 不显示用户ui变化
        if (that.mTableStatus.LeftPlayTimes == 0 && that.mTableStatus.Step == 0){
            return;
        }

        //收到大结算了 不处理用户变化
        if (mo.nnTable.mNnTableRoomEndLayer != null){
            return;
        }

        if (that.mTableStatus.TableID){
            var infos = [null, null, null, null, null];
            for (var seat = 0; seat < 5; seat++){
                var logicSeat = that.getLogicForView(seat, that.mTableStatus.MySeat - 1);
                infos[seat] = that.mTableStatus["SeatUser" + (logicSeat + 1)];
            }
            mo.nnTable.mNnTableUILayer.setTablePlayerInfos(infos);
        }else{
            //录像可能没有
        }
    };

    //更新牌局信息
    that.updateStep = function(){
        if (that.mTableStatus.Step == 0){

        }else if (that.mTableStatus.Step == 1){
            //抢庄
        }else if (that.mTableStatus.Step == 2){
            //开牌
            if (that.mTableStatus.LastAction == null){
                var cardNumss = [];
                for (var seat = 0; seat < 5; seat++){
                    var logicSeat = that.getLogicForView(seat, that.mTableStatus.MySeat - 1);
                    var seatUser = that.mTableStatus["SeatUser" + (logicSeat + 1)];
                    if (seatUser != null){
                        var cardNums = seatUser.HandCards;
                        // while (cardNums.length != 5){
                        //     if (cardNums.length > 5){
                        //         cardNums.splice(cardNums.length, 1);
                        //     }else{
                        //         cardNums.splice(cardNums.length, 0, 0);
                        //     }
                        // }
                        cardNumss.push(cardNums);
                    }else{
                        cardNumss.push([]);
                    }
                }
                //下注
                var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
                mo.nnTable.mNnTableUILayer.setBetInfo(mySeatUser.NewActionList);

                //发牌动画
                mo.nnTable.setFapaiAnim(cardNumss);

                //如果是庄家就直接发送值
                if (mySeatUser.IsHost){
                    that.sendMyBet(0);
                }
            }

        }else if (that.mTableStatus.Step == 3){

            var lastAction = that.mTableStatus.LastAction;
            var lastSeat = that.mTableStatus.LastActionSeat;

            //重置
            if (lastAction == null || (lastAction != null && lastAction.code != 2)){
                var temp = [-1, -1, -1, -1, -1];
                mo.nnTable.setkaiPaiAnim(temp, temp);
                //开牌
                var cardNumss = [];
                for (var seat = 0; seat < 5; seat++){
                    var logicSeat = that.getLogicForView(seat, that.mTableStatus.MySeat - 1);
                    var seatUser = that.mTableStatus["SeatUser" + (logicSeat + 1)];
                    if (seatUser != null){
                        var cardNums = seatUser.HandCards;
                        cardNumss.push(cardNums);
                    }else{
                        cardNumss.push([]);
                    }
                }
                mo.nnTable.mNnTableCardLayer.setInhandCard(cardNumss, true);
            }

            //设置
            if (lastAction != null && lastAction.code == 2){
                var cardType = that.getCardType(lastAction.Rate);
                var cardNums = lastAction.Cards;
                var seat = that.getViewForLogic(lastSeat - 1, that.mTableStatus.MySeat - 1);
                mo.nnTable.setKaipaiOnePlayer(cardType, cardNums, seat, true);

                var sex = that.mTableStatus["SeatUser" + lastSeat].user_info.sex;
                mo.soundHelper.playNnCardType(sex, cardType);
            }
        }
    };

    that.setReconnet = function(){
        var tableStatus = mo.nnTableStatus.mTableStatus;

        mo.nnTable.updateLastPlayTimes();

        if (tableStatus.Step == 2){
            var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
            mo.nnTable.mNnTableUILayer.setBetInfo(mySeatUser.NewActionList);
        }

        if (tableStatus.Step == 2 || tableStatus.Step == 3){
            var cardNumss = [];
            for (var seat = 0; seat < 5; seat++){
                var logicSeat = that.getLogicForView(seat, that.mTableStatus.MySeat - 1);
                var seatUser = that.mTableStatus["SeatUser" + (logicSeat + 1)];
                if (seatUser != null){
                    var cardNums = seatUser.HandCards;
                    cardNumss.push(cardNums);
                }else{
                    cardNumss.push([]);
                }
            }
            mo.nnTable.mNnTableCardLayer.setInhandCard(cardNumss);
        }

        if (tableStatus.Step == 3){
            for (var seat = 0; seat < 5; seat++){
                var logicSeat = that.getLogicForView(seat, that.mTableStatus.MySeat - 1);
                var seatUser = that.mTableStatus["SeatUser" + (logicSeat + 1)];

                var isSelectCard = false;

                if (seatUser != null) {
                    var lastAction = seatUser.LastAction;
                    if (lastAction != null && lastAction.code == 2) {
                        var cardType = that.getCardType(lastAction.Rate);
                        var cardNums = lastAction.Cards;
                        mo.nnTable.setKaipaiOnePlayer(cardType, cardNums, seat);
                        isSelectCard = true;
                    }
                }

                if (!isSelectCard){
                    mo.nnTable.setKaipaiOnePlayer(-1, [], seat);
                }
            }
        }
    };

    //设置ui该显示的状态
    that.setUIShowStatus = function(){
        if (that.mTableStatus.Step == 0){
            //非牌局
            mo.nnTable.mNnTableUILayer.intoShowIndex(11);

            mo.nnTable.updateLastPlayTimes();
        }else if (that.mTableStatus.Step == 1){
            //抢庄
        }else if (that.mTableStatus.Step == 2){
            //选倍
            var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
            if (mySeatUser.NewActionList.length > 0){
                mo.nnTable.mNnTableUILayer.intoShowIndex(21);
            }else{
                mo.nnTable.mNnTableUILayer.intoShowIndex(22);
            }
        }else if (that.mTableStatus.Step == 3){
            //选牌型
            var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
            if (mySeatUser.NewActionList.length > 0){
                mo.nnTable.mNnTableUILayer.intoShowIndex(30);
            }else{
                mo.nnTable.mNnTableUILayer.intoShowIndex(31);
            }
        }
    };

    //设置自己可以选择牌的操作
    that.getMySelectCards = function(){
        if (that.mTableStatus.Step == 3){
            var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
            if (mySeatUser.NewActionList.length == 0){
                return null;
            }
            if (mySeatUser.LastAction != null && mySeatUser.LastAction.code == 2){
                return null;
            }

            return mySeatUser.NewActionList[0];
        }

        return null;
    };

    //选择自己下注筹码
    that.sendMyBet = function(index){
        if (that.mTableStatus.Step == 2){
            var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
            var newActionList = mySeatUser.NewActionList;
            if (newActionList.length > index){
                mo.gameMsgManager.sendActionRequestnn(newActionList[index].code, newActionList[index].Rate, newActionList[index].Cards);
            }
        }
    };

    //自动开牌
    that.sendKaiPai = function(){
        if (that.mTableStatus.Step == 3){
            var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
            var newActionList = mySeatUser.NewActionList;
            if (newActionList.length > 0){
                mo.gameMsgManager.sendActionRequestnn(newActionList[0].code, newActionList[0].Rate, newActionList[0].Cards);
            }
        }
    };

    //转换cardType
    that.getCardType = function(cardType){
        if (cardType <= 14){
            for (var i = 0; i < 14; i++){
                var localCard = 14 - i;
                if (localCard == cardType){
                    return i;
                }
            }
        }else{
            if (cardType == 17){
                return 12;
            }else if (cardType == 18){
                return 11;
            }else if (cardType == 19){
                return 13;
            }else if (cardType == 20){
                return 111;
            }else if (cardType == 21){
                return 110;
            }else if (cardType == 22){
                return 10;
            }else if (cardType == 41){
                return 0;
            } else if (cardType < 41){
                var temp = cardType - 23;
                temp = 17 - temp;
                var yushu = temp % 2;
                if (yushu == 0){
                    return parseInt(temp / 2) + 1;
                }else {
                    return parseInt(temp / 2) + 1 + 100;
                }
            }
        }

        return 0;
    };

    //开牌
    that.sendActionSelect = function(){
        var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
        if (mySeatUser.NewActionList.length > 0){
            var action = mySeatUser.NewActionList[mySeatUser.NewActionList.length - 1];
            mo.gameMsgManager.sendActionRequestnn(action.code, action.Rate, action.Cards);
        }
    };

    //获取自己是否准备
    that.getMyIsReady = function(){
        var seatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
        return seatUser.State == 1;
    };

    //获取玩家同ip信息
    that.getPlayerIpEqual = function(){
        var result = [];

        //记录ip
        var ipStruct = {};
        for (var i = 1; i <= 5; i++){
            var seatUser = that.mTableStatus["SeatUser" + i];
            if (seatUser != null && seatUser.user_info != null){
                var userInfo = seatUser.user_info;
                if (mo.gameHelper.getIsJson(userInfo.DeviceJson)) {
                    var deviceJson = JSON.parse(userInfo.DeviceJson);
                    var ip = deviceJson.ip;
                    if (ip != null && ip != "" && ip != "127.0.0.1") {
                        if (ipStruct[ip] == undefined) {
                            ipStruct[ip] = [];
                        }
                        ipStruct[ip].push(userInfo.nickname);
                    }
                }
            }
        }

        //分解出出现两次的用户
        for(var key in ipStruct){
            if (ipStruct.hasOwnProperty(key)) {
                if (ipStruct[key].length > 1){
                    var nicks = ipStruct[key];
                    for (var i = 0; i < nicks.length; i++){
                        var item = {};
                        item.nick = nicks[i];
                        item.ip = key;
                        result.push(item);
                    }
                }
            }
        }

        return result;
    };

    //通过ui的返回玩家视图座位号
    that.getViewSeatForUid = function (uid) {
        var logicIndex = -1;
        for (var i = 0; i < 5; i++){
            var seatUser = that.mTableStatus["SeatUser" + (i + 1)];
            if (seatUser != null){
                if (seatUser.user_info.userid == uid || seatUser.user_info.nickname == uid){
                    logicIndex = i;
                }
            }
        }
        if (logicIndex != -1){
            return that.getViewForLogic(logicIndex, that.mTableStatus.MySeat - 1);
        }else{
            return -1;
        }
    };

    //通过用户id获取用户性别
    that.getSexForUid = function(uid){
        var sex = 0;
        for (var i = 0; i < 5; i++){
            var seatUser = that.mTableStatus["SeatUser" + (i + 1)];
            if (seatUser != null && seatUser.user_info != null){
                if (seatUser.user_info.userid == uid || seatUser.user_info.nickname == uid){
                    sex = seatUser.user_info.sex;
                }
            }
        }
        return sex;
    };

    //返回玩家人数
    that.getPlayerCount = function(){
        var count = 0;
        for (var i = 1; i <=5; i++){
            var userSeat = that.mTableStatus["SeatUser" + i];
            if (userSeat != null){
                count++;
            }
        }
        return count;
    };

    //加载录像
    that.setReplayMsg = function(rePlaynn){
        that.replayMsgs = [];
        that.replayTypes = [];

        //设置录像时候自己的座位号
        var replayMySeat = 1;
        var roundEndedEvent = rePlaynn.Result;
        for(var i = 1; i <= 5; i++){
            if (roundEndedEvent["SeatUser" + i] != null){
                if (roundEndedEvent["SeatUser" + i].user_info != null){
                    if (roundEndedEvent["SeatUser" + i].user_info.userid == mo.gameUserInfo.uid){
                        replayMySeat = i;
                        break;
                    }
                }
            }
        }
        roundEndedEvent.MySeat = replayMySeat;

        //牌桌映像数据
        for (var i = 0; i < rePlaynn.Images.length; i++){
            var tableStatsnn = rePlaynn.Images[i];
            //修改MySeat
            tableStatsnn.MySeat = replayMySeat;

            that.replayMsgs.push(tableStatsnn);
            that.replayTypes.push(906);
        }

        //牌桌结算数据
        that.replayMsgs.push(rePlaynn.Result);
        that.replayTypes.push(908);
    };

    //播放录像
    that.playReplay = function(){
        if (that.replayMsgs.length > 0){
            var msg = that.replayMsgs.shift();
            var type = that.replayTypes.shift();

            var response = null;
            if (type == 906){
                //牌桌映象
                response = new NnTableStatsnn();

            }else if (type == 908){
                //牌桌结算
                response = new NnRoundEndedEventnn();

            }

            if (response){
                response.handleMsg(msg);
                response.log();
            }else{
                cc.warn("type:" + type + "未注册");
            }
            return true;
        }else{
            cc.log("录像播放完毕");
            return false;
        }

    };

    that.getLogicForView = function(view, myLogicSeat){
        var logic = (view + 5 + myLogicSeat) % 5;
        return logic;
    };

    that.getViewForLogic = function(logic, myLogicSeat){
        var view = (logic + 5 - myLogicSeat) % 5;
        return view;
    };

    return that;
};

mo.nnTableStatus = new NnTableStatus();